package com.fis.hthk.dblinkapi.util;

import java.math.BigDecimal;

import com.agileitp.forte.framework.Array;
import com.agileitp.forte.framework.TextData;
import com.ssaglobal.scm.wms.service.baseobjects.EXEDataObject;

public class WmsProcedure {

	//移动
	private static final String EJB_MOVE = "nspItrnAddMove";
	//分配
	private static final String EJB_ALLOCATE = "NSPORDERPROCESSING";
	//ASN全收
	private static final String EJB_RECEIVEALL = "NSPRECEIVEALL";
	//订单发运
	private static final String EJB_SHIPORDER = "NSPMASSSHIPORDERS";

	/**
	 * 调用WMAPP移动功能
	 * @param jndi 数据源JNDI
	 * @param uid  用户ID
	 * @param storerkey 货主
	 * @param sku  物料
	 * @param lot  批次
	 * @param fromid 源id
	 * @param fromloc 源货位
	 * @param toloc  目的货位
	 * @param toid   目的ID
	 * @param status 库存状态
	 * @param qty    移动数量
	 * @throws Exception
	 */
	public static void move(String provider, String jndi,String uid,String storerkey,String sku,String lot,String fromid,
			String fromloc,String toloc,String toid,String status,BigDecimal qty) throws Exception{
		  Array params = new Array();
		  TextData nullData = new TextData("null");
		  TextData zeroData = new TextData("0");
		  params.add(nullData);
			params.add(storerkey);
			params.add(sku);
			params.add(lot);
			params.add(fromid);
			params.add(fromloc);
			params.add(toloc);
			params.add(toid);
			params.add(status); //status
			params.add(nullData); //lottable1
			params.add(nullData);
			params.add(nullData);
			params.add(nullData);
			params.add(nullData);
			params.add(nullData);
			params.add(nullData);
			params.add(nullData);
			params.add(nullData);
			params.add(nullData); //lottable10
			params.add(zeroData);
			params.add(zeroData);
			params.add(qty);
			params.add(zeroData);
			params.add(zeroData);
			params.add(zeroData);
			params.add(zeroData);
			params.add(zeroData);
			params.add(zeroData);
			params.add(new TextData(""));
			params.add(new TextData("Console Move"));
			params.add(" ");
			params.add("EA");
			params.add(new TextData("1"));
			params.add(nullData);
			params.add(nullData); //itrnkey
			params.add(nullData); //success
			params.add(nullData); //err
		
		ExecProcedure.execProcedureAction(provider, jndi, uid, EJB_MOVE, params);
	}


	public static void allocate(String provider, String jndi,String uid,String orderKey) throws Exception{
		String  osKey="", doCarton="Y", doRoute="N", tblPrefix="", preallocateOnly="N";
		Array params = new Array();
		//Store parameters for stored procedure call
		params.add(new TextData(orderKey));
		params.add(new TextData(osKey));
		params.add(new TextData(doCarton));
		params.add(new TextData(doRoute));
		params.add(new TextData(tblPrefix));
		params.add(new TextData(preallocateOnly));
		
		ExecProcedure.execProcedureAction(provider, jndi, uid, EJB_ALLOCATE, params);
	}
	/**
	 * ASN全收
	 * @param jndi 数据源JNDI
	 * @param uid 用户ID
	 * @param ReceiptKey ASN单号
	 * @param ReceiptType ASN类型
	 * @throws Exception
	 */
	public static void receiveall(String provider, String jndi,String uid,String ReceiptKey,String ReceiptType) throws Exception{
		
		Array parms = new Array(); 
		parms.add(new TextData(ReceiptKey));
		parms.add(new TextData(ReceiptType));
		ExecProcedure.execProcedureAction(provider, jndi, uid, EJB_RECEIVEALL, parms);
	}
	/**
	 * 订单发运
	 * @param jndi 数据源JNDI
	 * @param uid 用户ID
	 * @param orderKey 订单号
	 * @throws Exception
	 */
	public static void shiporder(String provider, String jndi,String uid,String orderKey) throws Exception{
		
		Array parms = new Array(); 
		parms.add(new TextData(orderKey));
		ExecProcedure.execProcedureAction(provider, jndi, uid, EJB_SHIPORDER, parms);
	}
	/**
	 * 完成拣货
	 * @param jndi 数据源JNDI
	 * @param whseid 仓库ID
	 * @param pickdetailkey 拣货明细号
	 * @throws Exception
	 */


	/**
	   * 得到Base產生的各種流水號
	   * 
	   * @param jndi
	   * @param userid
	   * @param keyname
	   * @param fieldlength
	   * @param increment
	   * @return
	   * @throws Exception
	   */
	  public static String getKey(String provider, String jndi, String userid, String keyname,
	      String fieldlength, String increment) throws Exception {

	    String rtnString = new String();
	    String procedureName = "NSPG_GETKEY";
	    Array parms1 = new Array();
	    parms1.add(new TextData(keyname));
	    parms1.add(new TextData(fieldlength));
	    parms1.add(new TextData(increment));
	    EXEDataObject result;
	    try {
	      result = ExecProcedure.execProcedureAction(provider, jndi, userid, procedureName,
	          parms1);
	      rtnString = result.getAttribValue(new TextData("keyname")).getAsString();
	    } catch (Exception e) {
	      // TODO Auto-generated catch block
	      throw new Exception(e.getMessage());
	    }

	    return rtnString;

	  }	
}
